package bll

import (
	"context"

	"pay/model"
	"pay/model/entity"
	"pay/model/mapping"
	"pay/store"
	"pay/store/postgres"
	"time"
)

type feePlanGroupObject struct {
	iFeePlanGroupObject store.IFeePlanGroupObject
}

var FeePlanGroupObject = &feePlanGroupObject{
	iFeePlanGroupObject: postgres.FeePlanGroupObject,
}

func init() {
	Register(FeePlanGroupObject)
}

func (a *feePlanGroupObject) init() func() {
	return func() {}
}

// CreateInBatch
func (a *feePlanGroupObject) CreateInBatch(ctx context.Context, ins []*model.FeePlanGroupObjectCreateRequest, size int) error {
	var (
		err error
		es  []*entity.FeePlanGroupObject
	)

	for _, in := range ins {
		es = append(es, buildFeePlanGroupObject(in))
	}

	err = a.iFeePlanGroupObject.CreateInBatch(ctx, es, size)
	for i, _ := range es {
		ins[i].Id = es[i].Id
	}
	return err
}

// Create
func (a *feePlanGroupObject) Create(ctx context.Context, in *model.FeePlanGroupObjectCreateRequest) error {
	var (
		err error
	)
	c := buildFeePlanGroupObject(in)
	_, err = a.iFeePlanGroupObject.Create(ctx, c)
	return err
}

// Update
func (a *feePlanGroupObject) Update(ctx context.Context, in *model.FeePlanGroupObjectUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.Id != nil {
		dict["id"] = in.Id
	}

	if in.Detail != nil {
		dict["detail"] = in.Detail
	}

	if in.GroupId != nil {
		dict["group_id"] = in.GroupId
	}

	if in.ObjectId != nil {
		dict["object_id"] = in.ObjectId
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iFeePlanGroupObject.Update(ctx, *in.Id, dict)
}

// Delete
func (a *feePlanGroupObject) Delete(ctx context.Context, in *model.FeePlanGroupObjectDeleteRequest) error {

	return a.iFeePlanGroupObject.Delete(ctx, in.Id)
}

// DeleteByGroupId
func (a *feePlanGroupObject) DeleteByGroupId(ctx context.Context, in *model.FeePlanGroupObjectDeleteByGroupIDRequest) error {

	return a.iFeePlanGroupObject.DeleteByGroupId(ctx, in.GroupID)
}

// List
func (a *feePlanGroupObject) List(ctx context.Context, in *model.FeePlanGroupObjectListRequest, opts ...model.QueryOptions) (*model.FeePlanGroupObjectListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.FeePlanGroupObject
		out   = &model.FeePlanGroupObjectListResponse{}
	)

	if total, list, err = a.iFeePlanGroupObject.List(ctx, in, opts...); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.FeePlanGroupObjectsEntityToDto(list)

	return out, nil
}

// Find
func (a *feePlanGroupObject) Find(ctx context.Context, in *model.FeePlanGroupObjectInfoRequest) (*model.FeePlanGroupObjectInfo, error) {
	var (
		err  error
		data *entity.FeePlanGroupObject
		out  = &model.FeePlanGroupObjectInfo{}
	)

	if data, err = a.iFeePlanGroupObject.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.FeePlanGroupObjectEntityToDto(data)
	return out, nil
}

// buildFeePlanGroupObject build entity
func buildFeePlanGroupObject(in *model.FeePlanGroupObjectCreateRequest) *entity.FeePlanGroupObject {

	ety := &entity.FeePlanGroupObject{}

	ety.Id = in.Id

	ety.GroupId = in.GroupId

	ety.Detail = in.Detail

	ety.ObjectId = in.ObjectId

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
